package com.flyco.modules.ums.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.flyco.modules.stm.entity.StmStoreInfo;
import com.flyco.modules.ums.entity.UmsMemberParam;
import com.flyco.modules.ums.mapper.UmsMemberParamMapper;
import com.flyco.modules.ums.mapper.UmsMemberReceiveAddressMapper;
import com.flyco.modules.ums.mapper.UmsMemberTagMapper;
import com.flyco.modules.ums.model.UmsMemberReceiveAddressModel;
import com.flyco.modules.ums.service.IUmsMemberParamService;
import com.flyco.modules.ums.service.IUmsMemberReceiveAddress;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UmsMemberReceiveAddressServiceImpl extends ServiceImpl<UmsMemberReceiveAddressMapper, UmsMemberReceiveAddressModel> implements IUmsMemberReceiveAddress {

    @Autowired
    private UmsMemberReceiveAddressMapper umsMemberReceiveAddressMapper;

    public boolean InsertAddressWhenStoreInfoCreate(StmStoreInfo stmStoreInfo) {

        UmsMemberReceiveAddressModel model = new UmsMemberReceiveAddressModel();
        model = SetReceiveAddressModel(stmStoreInfo, model);
        return save(model);
    }

    public boolean updateAddressWhenStoreInfoUpdate(StmStoreInfo stmStoreInfo) {

        //如果在ums_member_receive_address中没有对于的记录，则在表中增加一条默认地址
        QueryWrapper qw = new QueryWrapper();
        qw.eq("member_id", stmStoreInfo.getStoreId());
        qw.eq("default_status", 1);
        List<UmsMemberReceiveAddressModel> addressModelList = list(qw);

        if (CollectionUtils.isEmpty(addressModelList)) {
            //否则进行插入
            return InsertAddressWhenStoreInfoCreate(stmStoreInfo);
        } else {
            UmsMemberReceiveAddressModel model = addressModelList.get(0);
            //进行更新；
            model = SetReceiveAddressModel(stmStoreInfo, model);
            return updateById(model);
        }
    }

    private UmsMemberReceiveAddressModel SetReceiveAddressModel(StmStoreInfo stmStoreInfo, UmsMemberReceiveAddressModel model) {

        model.setMemberId(stmStoreInfo.getStoreId());
        model.setName(stmStoreInfo.getLinkName());
        model.setPhoneNumber(stmStoreInfo.getPhone());
        model.setDefaultStatus(1);
        model.setProvince(stmStoreInfo.getProvince());
        model.setCity(stmStoreInfo.getCity());
        model.setRegion(stmStoreInfo.getDistrict());
        model.setDetailAddress(stmStoreInfo.getAddress());
        model.setProvinceCode(stmStoreInfo.getProvinceCode());
        model.setCityCode(stmStoreInfo.getCityCode());
        model.setAreaCode(stmStoreInfo.getDistrictCode());
        model.setPhone2(stmStoreInfo.getPhone2());
        model.setDistance(stmStoreInfo.getDistance());
        model.setTown(stmStoreInfo.getTown());
        model.setTownCode(stmStoreInfo.getTownCode());
        return model;
    }

    @Override
    public UmsMemberReceiveAddressModel getDefaultAddressByMemberId(Long memberId) {

        LambdaQueryWrapper<UmsMemberReceiveAddressModel> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(UmsMemberReceiveAddressModel::getMemberId, memberId)
                .eq(UmsMemberReceiveAddressModel::getDefaultStatus, 1);
        return umsMemberReceiveAddressMapper.selectOne(wrapper);
    }

}
